package com.bootdo.system.controller;

import com.bootdo.common.controller.BaseController;
import com.bootdo.common.utils.Query;
import com.bootdo.system.domain.DeptDO;
import com.bootdo.system.service.BeautyDeptService;
import com.bootdo.system.service.DeptService;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.util.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 最美班级
 *
 * @author zhouxm
 */
@Controller
@RequestMapping("/system/beauty")
@Slf4j
public class BeautyController extends BaseController {

    @Autowired
    private DeptService deptService;

    @Autowired
    private BeautyDeptService beautyDeptService;

    /***
     * 周美丽班级列表页面
     * @return
     */
    @GetMapping()
    @RequiresPermissions("system:beauty:beauty")
    public String toBeauty() {
        return "system/dept/beauty";
    }

    /**
     * 获取周美丽班级列表数据
     * @param params
     * @return
     */
    @ResponseBody
    @GetMapping("/list")
    @RequiresPermissions("system:beauty:beauty")
    public List<DeptDO>list(@RequestParam Map<String, Object> params) {
        //查询列表数据
        setDateQuery(params);
        //params.put("parentId","0");
        List<DeptDO> list1 = deptService.list(params);
        /*List<DeptDO> list2 = new ArrayList<DeptDO>();
        if(!CollectionUtils.isEmpty(list1)) {
            list1.forEach(data1 -> {
                params.put("parentId",data1.getDeptId());
                List<DeptDO> deptList = deptService.beautyList(params);
                if(!CollectionUtils.isEmpty(deptList)) {
                    list2.add(data1);
                    list2.addAll(deptList);
                }
            });
        }*/
        return list1;
    }

    @GetMapping("/view/{id}")
    @RequiresPermissions("system:beauty:view")
    public String toView(@PathVariable("id") String id, Model model) {
        model.addAttribute("deptId", id);
        return "system/dept/score";
    }

    /***
     * 学期美丽班级列表页面
     * @return
     */
    @GetMapping("/beautyClass")
    public String toBeautyClass() {
        return "system/dept/beautyClass";
    }

    /**
     * 获取学期美丽班级列表数据
     * @param params
     * @return
     */
    @ResponseBody
    @GetMapping("/beautyClassList")
    public List<DeptDO>beautyClassList(@RequestParam Map<String, Object> params) {
        //查询列表数据
        setDateQuery(params);
        params.put("parentId","0");
        List<DeptDO> list1 = deptService.list(params);
        List<DeptDO> list2 = new ArrayList<DeptDO>();
        if(!CollectionUtils.isEmpty(list1)) {
            list1.forEach(data1 -> {
                params.put("parentId",data1.getDeptId());
                List<DeptDO> deptList = beautyDeptService.beautyClassList(params);
                if(!CollectionUtils.isEmpty(deptList)) {
                    list2.add(data1);
                    list2.addAll(deptList);
                }
            });
        }
        return list2;
    }
}
